<?php

namespace App\Http\Controllers\Home;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\DB;
use App\Models\TopicBbs;
use App\Models\Tag;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cache;
use App\Models\Remind;

class IndexController extends Controller
{
    //首页
    public function index(Request $request){
        $remind=0;
        if ($request->session()->has('user')){
            $request->session()->put('userModel',$request->session()->get('user'));
            if ($request->session()->get('online')==1){
                Cookie::queue('test', $request->session()->get('user'), 10);
            }
            $remind=count(Remind::where('another_id',$request->session()->get('userModel')->id)->where('read',0)->get());
        }
        else{
            if (isset($_COOKIE['test'])){
                $request->session()->put('userModel',Cookie::get('test'));
                $remind=count(Remind::where('another_id',$request->session()->get('userModel')->id)->where('read',0)->get());
            }
        }

        error_reporting(0);
        $topic_list = TopicBbs::orderBy('section','desc')->paginate(20);

        return view('home/IndexViews/index', ['topics' => $topic_list,'remind'=>$remind]);
    }
    //登陆界面
    public function login_view(){
        return view('home/AuthViews/login');
    }
    //注册界面
    public function register_view(){
        return view('home/AuthViews/register');
    }
    //登出
    public function land_out(Request $request){
        $request->session()->flush();
        Cookie::queue('test', null , -1);
        return Redirect('');
    }
    //搜索
    public function search(Request $request){
        $value=trim($request->input('search_value'));
        if ($value==''){
            return redirect('index')->with('red_msg','搜索条件不能为空');
        }
        $user=User::where('username','like','%'.$value.'%')->with('profile','another','topic','reply')->get();
        $topic=TopicBbs::where('content','like','%'.$value.'%')->orwhere('title','like','%'.$value.'%')->with('profile','vote','replies','user')->orderBy('view_num','desc')->get();
        return view('home/IndexViews/search',['user'=>$user,'topic'=>$topic,'value'=>$value]);
    }
}
